//
//  UserListViewController.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "UserListViewController.h"
#import "UserListView.h"
#import "ActivityCollectViewController.h"
#import "LoginViewController.h"
#import "FileHandle.h"
@interface UserListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UserListView *userListView;
@property (nonatomic,strong)NSArray *array;

@end

@implementation UserListViewController

- (void)loadView
{
    self.userListView = [[UserListView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.userListView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    
    self.userListView.tableView.dataSource = self;
    self.userListView.tableView.delegate = self;
    
    self.array = @[@"我的活动",@"我的电影",@"清除缓存"];
    //右侧登陆按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;

    self.barBtnItem = self.navigationItem.rightBarButtonItem;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([FileHandle shareInstance].loginState == YES) {
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }else{
        self.navigationItem.rightBarButtonItem.title = @"登陆";
    }
}


//右侧登陆按钮方法
- (void)rightItemAction:(UIBarButtonItem *)sender
{

    if ([sender.title isEqualToString:@"登陆"]) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *loginNC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:nil];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认注销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[FileHandle shareInstance] setloginState:NO];
        [[FileHandle shareInstance] synchronize];
        self.navigationItem.rightBarButtonItem.title = @"登陆";
    
    }
}

#pragma mark - UITableViewDataSource方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}



// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_id = @"cell_id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }
    cell.textLabel.text = self.array[indexPath.row];
    
    
    return cell;
}

#pragma mark - UITableViewDelegate方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        ActivityCollectViewController *acVC = [[ActivityCollectViewController alloc]init];
        NSArray *arr = [[FileHandle shareInstance] arrayMovie];
        NSLog(@"%ld",arr.count);
        acVC.title = @"电影收藏";
        acVC.array = [NSMutableArray arrayWithArray:arr];
        [self.navigationController pushViewController:acVC animated:YES];
    }
   
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
